package com.scitc.pleasurevideo.util;

import android.app.Activity;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.scitc.pleasurevideo.data.DataUtil;
import com.scitc.pleasurevideo.okgoutil.HttpCompleteOrFail;
import com.scitc.pleasurevideo.okgoutil.OkGoUtil;

import java.util.ArrayList;
import java.util.List;

public class AtyContainer {

    public AtyContainer() {
    }

    private static AtyContainer instance = new AtyContainer();
    private static List<Activity> activityStack = new ArrayList<Activity>();

    public static AtyContainer getInstance() {
        return instance;
    }

    /**
     * 添加Activity到堆栈
     */
    public void addActivity(Activity activity) {
        if (activityStack == null) {
            activityStack = new ArrayList<Activity>();
        }
        activityStack.add(activity);
        isAppStart();
    }

    private void isAppStart() {
//        String version = DataUtil.getVersionName(activity);
//        OkGo.<String>get("http://119.23.249.61/programBackstage/isStartApp?version=" + version + "&name=pleasurevideo")
//                .tag(activity)
//                .execute(new StringCallback() {
//                    @Override
//                    public void onSuccess(Response<String> response) {
//                        if (!("true".equals(response.body()))) {
//                            finishAllActivity();
//                        }
//                    }
//                });
        OkGoUtil.init().isAppStart(new HttpCompleteOrFail() {
            @Override
            public void onComplete(String json) {
                if (!("true".equals(json))) {
                    finishAllActivity();
                }
            }

            @Override
            public void onFile(String information) {
                finishAllActivity();
            }
        });
    }


    /**
     * 移除指定的Activity
     */
    public void removeActivity(Activity activity) {
        if (activity != null) {
            activityStack.remove(activity);
        }
    }

    /**
     * 结束所有Activity
     */
    public void finishAllActivity() {
        for (int i = 0, size = activityStack.size(); i < size; i++) {
            if (null != activityStack.get(i)) {
                activityStack.get(i).finish();
            }
        }
        activityStack.clear();
    }

}
